package com.zr.lagalplant.service.impl;

import com.zr.lagalplant.mapper.LegalPersonMapper;
import com.zr.lagalplant.pojo.*;
import com.zr.lagalplant.service.LegalPersonService;
import com.zr.meiju.StatusEnum;
import com.zr.util.AllRecords;
import com.zr.util.ResultVo;
import com.zr.util.ResultVoBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/10/8.
 */
@Service
public class LegalPersonServiceImpl implements LegalPersonService{
    @Autowired
    private LegalPersonMapper legalPersonMapper;
    @Override
    public ResultVo querypage(@Valid LegalPlantSelectVo legalPlantSelectVo) {
        //根据条件查询数量
        int count=legalPersonMapper.querycount(legalPlantSelectVo);
        //根据条件查询数据
        List<LegalPlantVo> legalPlantVos=legalPersonMapper.querydata(legalPlantSelectVo);
        //将数据封装进ALLrecords
        AllRecords allRecords=new AllRecords();
        allRecords.setPageIndex(legalPlantSelectVo.getPageIndex());
        allRecords.setPageSize(legalPlantSelectVo.getPageSize());
        allRecords.resetTotalPage(count);
        allRecords.setTotalNumber(count);
        allRecords.setDataList(legalPlantVos);
        return ResultVoBuilder.success(allRecords);
    }

    @Override
    public ResultVo queryLegalPersonByUserId(Integer userId) {
        if (userId==null){
            //如果session中没有拿到信息就让用户重新登录
        }
        List<LegalPerson> legalPeople = legalPersonMapper.queryLegalPersonByUserId(userId);
        return ResultVoBuilder.success(legalPeople);
    }

    //添加  验证
    //验证法人编码是否存在
    //验证是否工厂编码重复
    @Override
    public ResultVo add(@Valid LegalPlantAddVo legalPlantAddVo) {
        //验证信息
       ResultVo resultVo=yanzhengcunzai(legalPlantAddVo);
       //如果验证失败，返回验证阶段
       if(!resultVo.getSuccess()){
           return  resultVo;
       }
       //验证成功后
        //新建工厂
        LegalPlantVo legalPlantVo=new LegalPlantVo();
        BeanUtils.copyProperties(legalPlantAddVo,legalPlantVo);
        //给创建人，创建时间赋值
        Date date=new Date();
        legalPlantVo.setCreateName("小创");
        legalPlantVo.setCreateTime(date);
        legalPersonMapper.add(legalPlantVo);
        return  ResultVoBuilder.success();

    }

    @Override
    public ResultVo updateLegalPlant(@Valid LegalPlantUpdateVo legalPlantUpdateVo) {
        //验证要修改的数据是否存在
        List<LegalPlantVo> plantVoList = legalPersonMapper.queryByCode(legalPlantUpdateVo.getLegalPlantCode());
        if (CollectionUtils.isEmpty(plantVoList)){
            return ResultVoBuilder.error("500","修改的数据不存在！");
        }
        //验证是否是禁用状态的数据
        Integer enabled = plantVoList.get(0).getEnabled()?1:0;
        if (enabled== StatusEnum.QIYONG.getStatusValue()){
            return ResultVoBuilder.error("500","启用状态的数据不能修改！");
        }
        //给修改人员和修改时间赋值
        LegalPlantVo plantVo1 = new LegalPlantVo();
        BeanUtils.copyProperties(legalPlantUpdateVo, plantVo1);
        plantVo1.setUpdateName("小改");
        plantVo1.setUpdateTime(new Date());

        //修改语句。
        legalPersonMapper.update(plantVo1);
        return ResultVoBuilder.success();
    }

    @Override
    public ResultVo updateStatus(@Valid LegalPlantStatusVo legalPlantStatusVo) {
        //验证要修改的数据是否存在
        LegalPlantVo plantVo = legalPersonMapper.queryById(legalPlantStatusVo.getLegalPlantCode());
        if (plantVo ==null){
            return ResultVoBuilder.error("500","修改的数据不存在！");
        }
        //给修改人员和修改时间赋值
        LegalPlantVo plantVo1 = new LegalPlantVo();
        BeanUtils.copyProperties(legalPlantStatusVo, plantVo1);
        plantVo1.setUpdateName("小改");
        plantVo1.setUpdateTime(new Date());
        legalPersonMapper.updateStatus(plantVo1);

        return ResultVoBuilder.success();
    }

    private ResultVo yanzhengcunzai(@Valid LegalPlantAddVo legalPlantAddVo) {
        //验证法人编码是否存在
        //先从数据库对添加的法人编码进行查询
        List<LegalPerson> Codelist=legalPersonMapper.queryLegalPersonCode(legalPlantAddVo.getLegalPersonCode());
        if(Codelist.isEmpty()){
            return ResultVoBuilder.error("500","法人编码不存在");
        }
        //验证是否编码重复
        List<LegalPlantVo> PersonCodeList=legalPersonMapper.querylegalPlantCode(legalPlantAddVo.getLegalPlantCode());
        if(!PersonCodeList.isEmpty()){
            return  ResultVoBuilder.error("500","工厂编码已经存在");
        }
        return ResultVoBuilder.success();
    }
}
